package com.ge.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期处理
 * <p>
 * Created by wuye on 2018/1/11.
 */
public class DateUtils {
    /**
     * 时间格式(yyyy-MM-dd)
     */
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }

    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    //判断是否超过24小时
    public static boolean jisuan(String date1, String date2) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d HH:mm:ss");
        Date start = sdf.parse(date1);
        Date end = sdf.parse(date2);
        long cha = end.getTime() - start.getTime();
        double result = cha * 1.0 / (1000 * 60 * 60);
        if (result <= 24) {
            //System.out.println("可用");
            return true;
        } else {
            //System.out.println("已过期");
            return false;
        }
    }

    /**
     * 给时间加上几个小时
     *
     * @param day  当前时间 格式：yyyy-MM-dd HH:mm:ss
     * @param hour 需要加的时间
     * @return
     */
    public static String addDateMinut(String day, int hour) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(day);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (date == null) {
            return "";
        }
        System.out.println("front:" + format.format(date)); //显示输入的日期
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, hour);// 24小时制
        date = cal.getTime();
        System.out.println("after:" + format.format(date));  //显示更新后的日期
        cal = null;
        return format.format(date);

    }

    /**
     * 比较时间大小 string
     *
     * @param DATE1    前一个时间
     * @param DATE2    后一个时间
     * @param dateType
     * @return
     */
    public static Boolean string_compare_date(String DATE1, String DATE2, String dateType) {
        DateFormat df = new SimpleDateFormat(dateType);
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                System.out.println("大于后者");
                return true;
            } else if (dt1.getTime() < dt2.getTime()) {
                System.out.println("小于后者");
                return false;
            } else {
                return false;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return false;
    }

    /**
     * 比较时间大小 date
     *
     * @param date
     * @param date1
     * @return
     */
    public static Boolean date_compare_date(Date date, Date date1) {
        try {
            if (date.getTime() > date1.getTime()) {
                System.out.println("大于后者");
                return true;
            } else if (date.getTime() < date1.getTime()) {
                System.out.println("小于后者");
                return false;
            } else {
                return false;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return false;
    }

    /**
     * 日期加算
     *
     * @param date
     * @param days
     * @return
     */
    public static Date dataAdd(Date date, Integer days) {
        Date dt1 = null;
        try {
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(date);
            rightNow.add(Calendar.DAY_OF_YEAR, days);//日期加10天
            dt1 = rightNow.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt1;
    }

    /**
     * 字符串转date
     *
     * @param dates
     * @return
     */
    public static Date toDate(String dates, String dateType) {
        Date dt1 = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(dateType);
            dt1 = sdf.parse(dates);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt1;
    }


    /**
     * date转字符串
     *
     * @param date
     * @param dateType
     * @return
     */
    public static String dateToString(Date date, String dateType) {
        String dates = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(dateType);
            dates = sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dates;
    }

    /**
     * date转字符串
     *
     * @param date
     * @return
     */
    public static String dateToString(Date date) {
        String dates = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            dates = sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dates;
    }

    /**
     * 获取两个时间相差的天数
     *
     * @param str1 时间参数 1：
     * @param str2 时间参数 2：
     * @return 相差天数
     */
    public static long getDistanceDays(String str1, String str2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date one;
        Date two;
        long days = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            days = diff / (1000 * 60 * 60 * 24);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    public static void main(String[] args) {
        String s = dateToString(new Date(),"HH:mm:ss");
        System.out.println(s);
        DateUtils.dataAdd(new Date(), 20);
    }

}
